package com.example.views;

import com.example.interfaces.IRegisterActivity;
import com.example.presenters.RegisterPresenter;
import com.example.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

/**
 * This is the RegisterActivity. It populates the form users will need to create
 * a new account to be able to use the app. When the user is created, it will be
 * added to the user list.
 * 
 * @author 3Factorial
 */
public class RegisterActivity extends Activity implements IRegisterActivity {

	private RegisterPresenter myPresenter;

	/**
	 * Activates the register activity when it is called.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		myPresenter = new RegisterPresenter(this,
				com.example.model.UserList.getInstance());
	}

	/**
	 * Builds up the options menu.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * Gets the basic user info, and uses it to create the user.
	 * 
	 * @param view
	 */
	public void register(View view) {
		String username = getUserName();
		String password = getPassword();
		myPresenter.register(username, password);
	}

	/**
	 * Gets the desired user name.
	 * 
	 * @return
	 */
	private String getUserName() {
		return ((EditText) findViewById(R.id.text_username)).getText()
				.toString();
	}

	/**
	 * Gets the desired password.
	 * 
	 * @return
	 */
	private String getPassword() {
		return ((EditText) findViewById(R.id.text_password)).getText()
				.toString();
	}

	/**
	 * Cancels the registering process.
	 * 
	 * @param view
	 *            The new view.
	 */
	public void cancel(View view) {
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
	}

	/**
	 * Moves the register activity to the profile activity after it is
	 * successful.
	 * 
	 * @param userName
	 */
	public void goToProfile(String userName) {
		Intent intent = new Intent(this, ProfileActivity.class);
		intent.putExtra(MainActivity.USERNAME, userName);
		startActivity(intent);
	}

	/**
	 * Displays a message saying the desired username is already taken.
	 */
	public void dispUsernameTaken() {
		TextView tv = (TextView) findViewById(R.id.login_error_text);
		tv.setText("Username Taken");
	}
	
	/**
	 * Displays a message saying the desired username is invalid.
	 */
	public void dispUsernameInvalid() {
		TextView tv = (TextView) findViewById(R.id.login_error_text);
		tv.setText("Invalid Username");
	}
	
	/**
	 * Displays a message saying the desired password is invalid.
	 */
	public void dispPasswordInvalid() {
		TextView tv = (TextView) findViewById(R.id.login_error_text);
		tv.setText("Invalid Password");
	}

}
